var dataObj;
function mail_edit(obj){
	jQ(obj).parents('ul').find('input.mail').toggle();
	jQ(obj).parents('ul').find('span.describe').toggle();
	jQ(obj).parents('li').find('a:eq(0)').nextAll().toggle();
}

function setValue(index,value){
	if((typeof parseInt(index,10))=="number")
		jQ("fieldset.info_base ul li").eq(index).find("span").eq(1).empty().append(value);
}
function okedit(){
	var baseInfoLi=jQ("fieldset.info_base ul li");
	var name=baseInfoLi.eq(0).find("input").val();
	name=jQ.trim(name);
	var sex="";
	baseInfoLi.eq(1).find("input").each(function(){
		if(jQ(this).attr("checked"))
			sex=jQ(this).val();
	});
	var birth="";
	baseInfoLi.eq(2).find("select").each(function(){
		birth+=jQ(this).val()+"-";
	});
	var qq=baseInfoLi.eq(3).find("input").val();
	qq=jQ.trim(qq);
	var msn=baseInfoLi.eq(4).find("input").val();
	msn=jQ.trim(msn);
	/*var mobile=baseInfoLi.eq(5).find("input").val();
	mobile = jQ.trim(mobile);*/
	var living=baseInfoLi.eq(6).find("input").val();
	living = jQ.trim(living);
	var job=baseInfoLi.eq(7).find("select").val();
	job = jQ.trim(job);
	var edu=baseInfoLi.eq(8).find("select").val();
	edu = jQ.trim(edu);
	var blog=baseInfoLi.eq(9).find("input").val();
	blog = jQ.trim(blog);
	var p = /[A-Za-z0-9\u4e00-\u9fa5]{2,20}$/;
	if(!p.test(name)){
		clip_tips("名称错误");
		return;
	}
	if(!(typeof parseInt(sex,10))=="number"){
		clip_tips("性别错误");
		return;
	}
	p=/^([1-2]\d{3})[\-](0?[1-9]|10|11|12)[\-]([1-2]?[0-9]|0[1-9]|30|31)[\-]$/;
	if(birth.replace(/-/g,"").length>0&&!p.test(birth)){
		clip_tips("出生日期错误");
		return;
	}
	p=/^[1-9][0-9]{4,9}$/;
	if(qq.length>0&&!p.test(qq)){
		clip_tips("QQ错误");
		return;
	}
	p=/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(msn.length>0&&!p.test(msn)){
		clip_tips("msn错误");
		return;
	}
	/*p=/^[1][1-9][0-9]\d{8}$/;
	if(mobile.length>0&&!p.test(mobile)){
		clip_tips("手机错误");
		return;
	}*/
	var strRegex = "^(http://)+(([0-9a-zA-Z_]+[\.]+[0-9a-zA-Z_])?([0-9a-zA-Z_]))+[\.]+([a-zA-Z]{2,3})+.+$";
	var re = new RegExp(strRegex);
	if(blog.length>0&&!re.test(blog)){
		clip_tips("个人网站格式错误 请输入以http://开头的URL");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!saveUserInfo.action",
		data:{
			nameInput:name,
			sexInput:sex,
			birthInput:birth,
			qqInput:qq,
			msnInput:msn,
			livingInput:living,
			jobInput:job,
			eduInput:edu,
			blogInput:blog
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['flag']){
					initUserInfo();
					checkExistUser();
					jQ("span.fix a").show();
					jQ("div.edit").hide();
				}
				clip_tips(dataObj['result']);
			}
		}
	});
}
function canceledit(){
	setValue(0,dataObj['name']);
	setValue(1,dataObj['sex']==""?"":(dataObj['sex']=="1"?"男":"女"));
	setValue(2,dataObj['birth']);
	setValue(3,dataObj['qq']);
	setValue(4,dataObj['msn']);
	setValue(5,dataObj['mobile']);
	setValue(6,dataObj['living']);
	setValue(7,dataObj['job']);
	setValue(8,dataObj['edu']);
	setValue(9,dataObj['blog']);
	jQ("span.fix a").show();
	jQ("div.edit").hide();
}
function toedit(obj){
	//设置名称
	setValue(0,"<input type=\"text\" value=\""+dataObj['name']+"\"/>");
	var Html="<input type=\"radio\" name=\"sex\" value=\"1\"/><span>男</span><input type=\"radio\" name=\"sex\" value=\"0\"/><span>女</span>";
	//设置性别
	setValue(1,Html);
	if(dataObj['sex']!=""){
		var sexinput=jQ("fieldset.info_base ul li").eq(1).find("input");
		if(dataObj['sex']=="1"){
			sexinput.eq(0).attr("checked","true");
		}else if(dataObj['sex']=="0"){
			sexinput.eq(1).attr("checked","true");
		}
	}
	//设置生日
	Html="<select onchange=\"updateDay()\"></select><span>年</span><select onchange=\"updateDay()\"></select><span>月</span><select></select><span>日</span>";
	setValue(2,Html);
	var select=jQ("ul li span.fix select");
	var birth=dataObj['birth'].split("-");
	select.eq(0).empty().append(genyear()).val(parseInt(birth[0],10));
	select.eq(1).empty().append(genMon()).val(parseInt(birth[1],10));
	select.eq(2).empty().append(genday()).val(parseInt(birth[2],10));
	//设置qq
	setValue(3,"<input type=\"text\" value=\""+dataObj['qq']+"\"/>");
	//设置smn
	setValue(4,"<input type=\"text\" value=\""+dataObj['msn']+"\"/>");
	//设置手机号
	setValue(5,"<input type=\"text\" value=\""+dataObj['mobile']+"\"/>");
	
	setValue(6,"<input type=\"text\" value=\""+dataObj['living']+"\"/>");
	setValue(7,createSelectHtml(['在校学生','企业职员','事业单位员工','公务员','自由职业者','无业/待业','其他'],dataObj['job']));
	setValue(8,createSelectHtml(['小学及以下','初中','高中','大专','本科','研究生','博士及以上'],dataObj['edu']));
	setValue(9,"<input type=\"text\" value=\""+dataObj['blog']+"\"/>");
	jQ(obj).hide();
	jQ("div.edit").show();
}
function createSelectHtml(arr,value){
	var html = "<select>";
	jQ.each(arr,function(i,dat){
		html += "<option value='"+dat+"' "+(value==dat ? "selected='selected'" : "")+">"+dat+"</option>";
	});
	html += "</select>";
	return html;
}
function updateDay(){
	var select=jQ("ul li span.fix select");
	var day=parseInt(jQ(select[2]).val(),10);
	select.eq(2).empty().append(genday()).val(day);
}
function genyear(){
	var html="<option value=\"\">选择</option>";
	for(var i=2011;i>1887;i--){
		html+="<option value=\""+i+"\">"+i+"</option>";
	}
	return html;
}
function genMon(){
	var html="<option value=\"\">选择</option>";
	for(var i=1;i<13;i++){
		html+="<option value=\""+i+"\">"+i+"</option>";
	}
	return html;
}
function genday(){
	var html="<option value=\"\">选择</option>";
	var select=jQ("ul li span.fix select");
	var year=parseInt(jQ(select[0]).val(),10);
	var month=parseInt(jQ(select[1]).val(),10);
	var j=0;
	if(year%4==0&&month==2){
		j=29
	}else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
		j=31;
	}else if(month==4||month==6||month==9||month==11){
		j=30;
	}else if(month==2){
		j=28
	}
	for(var i=1;i<=j;i++){
		html+="<option value=\""+i+"\">"+i+"</option>";
	}
	return html;
}
function editEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("span").eq(1).attr("title");
	var email = li.eq(1).find("span").eq(1).text();
	var status = li.eq(2).find("input");
	if(email.length>0){
		li.eq(0).find("span").eq(1).empty().append("<input type=\"text\" value=\""+description+"\"/>");
		li.eq(1).find("span").eq(1).empty().append("<input type=\"text\" disabled=\"disabled\" value=\""+email+"\"/>");
		jQ(status).removeAttr("disabled");
		var pageHtml="";
		if(email!=dataObj['email'])
			pageHtml+="<a onclick=\"deleteEmail(this)\">删除</a>";
		pageHtml+="<a onclick=\"cancelEmail(this)\">取消</a><a onclick=\"saveEmail(this)\">确定</a>";
		jQ(obj).parent().empty().append(pageHtml);
	}
}
function cancelEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("span").eq(1);
	var email = li.eq(1).find("span").eq(1);
	var status = li.eq(2).find("input");
	
	description.empty().append(description.attr("title").length>=10?description.attr("title").substr(0,10):description.attr("title"));
	email.empty().append(email.attr("title"));
	jQ(status).attr("disabled","disabled");
	var cid=jQ(status).parent().find("span").attr("cid");
	jQ(status).attr("checked",(cid=="0"?false:true));
	var pageHtml="";
	if(email.attr("title")!=dataObj['email'])
		pageHtml+="<a onclick=\"deleteEmail(this)\">删除</a>";
	pageHtml+="<a onclick=\"editEmail(this)\">编辑</a>";
	jQ(obj).parent().empty().append(pageHtml);
}
function deleteEmail(obj){
	var li=jQ(obj).parent().siblings();
	var email = li.eq(1).find("span").eq(1);
	if(email.attr("title")==(email.text()||email.find("input").val())){
		if(email.text()==dataObj['email']){
			clip_tips("不能删除");
			return;
		}else{
			jalert("您确定删除"+email.attr("title")+"这个邮箱吗？",{emailInput:email.attr("title")}, deleteEmail_callback);
		}
	}
}

function deleteEmail_callback(event){
	jQ.ajax({
		type:"POST",
		url:"controlAjax!deleteEmail.action",
		data:{
			emailInput:event.data.emailInput
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['flag'])
					initEmailInfo();
				clip_tips(dataObj['result']);
			}
		}
	});
}

function saveEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description=li.eq(0).find("span").eq(1).find("input").val();
	description = jQ.trim(description);
	var email=li.eq(1).find("span").eq(1).find("input").val();
	email = jQ.trim(email);
	var status=li.eq(2).find("input").attr("checked");
	status=(status==true?1:0);
	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(!p.test(email)){
		clip_tips("Email错误");
		return;
	}
	if(typeof status!="number")
		return;
	if(description.length<=0||description.length>20){
		clip_tips("描述错误");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!saveEmail.action",
		data:{
			emailInput:email,
			descInput:description,
			status:status
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['flag'])
					initEmailInfo();
				clip_tips(dataObj['result']);
			}
		}
	});
}
function addEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("input").val();
	var email = li.eq(1).find("input").val();
	email = jQ.trim(email);
	description = jQ.trim(description);
	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(!p.test(email)){
		clip_tips("Email错误");
		return;
	}
	if(description.length<=0||description.length>20){
		clip_tips("描述错误");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!bindEmail.action",
		data:{
			emailInput:email,
			descInput:description
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				clip_tips(dataObj['result']);
				initEmailInfo();
			}
		}
	});
}
function resendEmail(obj){
	var li=jQ(obj).parent().siblings();
	var description = li.eq(0).find("span.describe").text();
	var email = li.eq(1).find("span.describe").text();
	email = jQ.trim(email);
	description = jQ.trim(description);
	var p = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/i;
	if(!p.test(email)){
		clip_tips("Email错误");
		return;
	}
	if(description.length<=0){
		clip_tips("描述错误");
		return;
	}
	jQ.ajax({
		type:"POST",
		url:"controlAjax!resendEmail.action",
		data:{
			emailInput:email,
			descInput:description
		},
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				clip_tips(dataObj['result']);
			}
		}
	});
}
function initUserInfo(){
	jQ.ajax({
		type:"POST",
		url:"controlAjax!userInfo.action",
		success:function(data){
			if(data!=null){
				dataObj=eval("("+data+")");
				if(dataObj['loginflag']){
					jQ("div.info_title span").eq(1).empty().append(dataObj['email']);
					setValue(0,dataObj['name']);
					setValue(1,dataObj['sex']==""?"":(dataObj['sex']=="1"?"男":"女"));
					setValue(2,dataObj['birth']);
					setValue(3,dataObj['qq']);
					setValue(4,dataObj['msn']);
					setValue(5,dataObj['mobile']);
					setValue(6,dataObj['living']);
					setValue(7,dataObj['job']);
					setValue(8,dataObj['edu']);
					setValue(9,dataObj['blog']);
				}
			}
		}
	});
}
/**
 * email : String
 * flag : int 是否接受
 * description : String 描述
 * type : int 类型
 * editflag : boolean 是否可编辑
 */
function createEmailContentHtml(email,flag,description,type,editflag){
	description=HTMLEnCode(description);
	var contentHtml="";
	contentHtml+="<fieldset class=\"add_mail\">";
	if(type==1)
		contentHtml+="<legend>注册邮箱</legend>";
	else if(type==2)
		contentHtml+="<legend>绑定邮箱</legend>";
	else if(type==3)
		contentHtml+="<legend>未验证邮箱</legend>";
	else if(type==4)
		contentHtml+="<legend>添加新邮箱</legend>";
	contentHtml+="<ul>";
	contentHtml+="<li>";
	contentHtml+="<span class=\"info_t\">描述：</span>";
	if(editflag)
		contentHtml+="<input type=\"text\" value=\""+(description.length>=10?description.substr(0,10):description)+"\"/>";
	else
		contentHtml+="<span class=\"describe\" title=\""+description+"\">"+(description.length>=10?description.substr(0,10):description)+"</span>";
	contentHtml+="</li>";
	
	contentHtml+="<li>";
	contentHtml+="<span class=\"info_t\">邮箱：</span>";
	if(editflag)
		contentHtml+="<input type=\"text\" value=\""+email+"\"/>";
	else
		contentHtml+="<span class=\"describe\" title=\""+email+"\">"+email+"</span>";
	contentHtml+="</li>";
	
	
	contentHtml+="<li>";
	contentHtml+="<span class=\"info_t\" cid=\""+flag+"\">&nbsp;</span>";
	if(type==3){
		contentHtml+="<span>验证中…</span>";
		contentHtml+="<a class=\"wrap\" onclick=\"resendEmail(this)\">重新发送验证</a>";
	}else if(type==4){
		contentHtml+="";
	}else{
		if(editflag){
			if(flag==1)
				contentHtml+="<input type=\"checkbox\" checked=\"checked\"/>";
			else
				contentHtml+="<input type=\"checkbox\" />";
		}else{
			if(flag==1)
				contentHtml+="<input type=\"checkbox\" disabled=\"disabled\" checked=\"checked\"/>";
			else
				contentHtml+="<input type=\"checkbox\" disabled=\"disabled\"/>";
		}
		contentHtml+="<span>设置为接收邮箱</span>";
	}
	contentHtml+="</li>";
	
	
	contentHtml+="<li>";
	if(type==1){
		contentHtml+="<a onclick=\"editEmail(this)\">编辑</a>";
	}else if(type==2||type==3){
		contentHtml+="<a onclick=\"deleteEmail(this)\">删除</a>";
		contentHtml+="<a onclick=\"editEmail(this)\">编辑</a>";
	}else if(type==4){
		contentHtml+="<a onclick=\"addEmail(this);\">添加</a>";
	}
	
	contentHtml+="</li>";
	contentHtml+="</ul>";
	contentHtml+="</fieldset>";
	return contentHtml;
}
function initEmailInfo(){
	jQ.ajax({
		type:"POST",
		url:"controlAjax!userEmail.action",
		success:function(data){
			if(data!=null){
				jQ.ajax({
					type:"POST",
					url:"controlAjax!userInfo.action",
					success:function(userInfo){
						if(userInfo!=null){
							dataObj=eval("("+userInfo+")");
							if(dataObj['loginflag']){
								jQ("div.info_title span").eq(1).empty().append(dataObj['email']);
								setValue(0,dataObj['name']);
								setValue(1,dataObj['sex']==""?"":(dataObj['sex']=="1"?"男":"女"));
								setValue(2,dataObj['birth']);
								setValue(3,dataObj['qq']);
								setValue(4,dataObj['msn']);
								//setValue(5,dataObj['mobile']);
								setValue(6,dataObj['living']);
								setValue(7,dataObj['job']);
								setValue(8,dataObj['edu']);
								setValue(9,dataObj['blog']);
								setMobileVale(dataObj.mobilenum);
								var contentHtml="";
								var tds=jQ("table.information td").eq(1).empty();
								jQ(data).find('email').each(function(){
									var email=jQ(this).find("Email").text();
									if(email==dataObj['email']){
										contentHtml=createEmailContentHtml(email,parseInt(jQ(this).find("Status").text(),10),jQ(this).find("Description").text(),1,false)
										tds.append(contentHtml);
									}else{//绑定邮箱
										if(parseInt(jQ(this).find("Bindable").text(),10)==1){
											contentHtml=createEmailContentHtml(email,parseInt(jQ(this).find("Status").text(),10),jQ(this).find("Description").text(),2,false)
											tds.append(contentHtml);
										}else{
											contentHtml=createEmailContentHtml(email,parseInt(jQ(this).find("Status").text(),10),jQ(this).find("Description").text(),3,false)
											tds.append(contentHtml);
										}
									}
								});
								tds.append(createEmailContentHtml("",0,"",4,true));
							}
						}
					}
				});
			}
		}
	});
}

function setMobileVale(mobilenum){
	if(jQ.trim(mobilenum).length==0){
		jQ("#modelchangebtn").hide();
		jQ(".editmode").show();
		jQ("#mobilenumipt").css({border:"1px solid #7f9db9"});
		jQ("#submitinfo").empty().hide();
		jQ("#mobilenumipt").attr("disabled","");
		
	}else{
		jQ("#orientmobilenum").val(mobilenum);
		jQ("#mobilenumipt").val(mobilenum);
		jQ("#modelchangebtn").show();
		jQ("#mobilenumipt").attr("disabled","disabled");
	}
	jQ("#sendCodebtn").unbind("click").bind("click",sendVerCodeConfirm);
}

function editMobile(obj){
	if(jQ(obj).hasClass("editon")){
		jQ(obj).removeClass("editon").text("修改");
		jQ(".editmode").hide();
		jQ("#mobilenumipt").css({border:"0px"});
		jQ("#mobilenumipt").attr("disabled","disabled").val(jQ("#orientmobilenum").val());
	}else{
		jQ("#mobilenumipt").attr("disabled","");
		jQ(obj).addClass("editon").text("取消");
		jQ(".editmode").show();
		jQ("#mobilenumipt").css({border:"1px solid #7f9db9"});
		jQ("#submitinfo").empty().hide();
	}
}

function sendVerCodeConfirm(){
	if(jQ("#modelchangebtn").css("display")!='none'){
		var oldnum=jQ("#orientmobilenum").val();
		var minput=jQ.trim(jQ("#mobilenumipt").val());
		if(!minput.match(mobilever)){
			showAjaxCallBackMsg(0,"请输入正确的手机号码。");
			return;
		}
		if(minput==oldnum){
			showAjaxCallBackMsg(0,"对不起，您输入的手机号已经绑定在您的账户上。");
			return;
		}
		var opt=jQ("#viewopt");
		opt.bind("sendVerCodeEvent",sendVerCode);
		opt.data("function","sendVerCodeEvent")
		jQ("#viewopt_p").empty().append("您确定要绑定新的手机号么？");
		opt.dialog( "option", "title","提醒" );
		opt.dialog("open");
	}else{
		sendVerCode(true);
	}
}

var mobilever = new RegExp(/((1(30|31|32|33|34|35|36|37|38|39|45|47|50|51|52|53|54|55|56|57|58|59|80|81|82|83|84|85|86|87|88|89)\d{8,8})|((106|0)\d{10,11}))/img);
var mobilecode=new RegExp(/(\d{6})/img);

function resetVerCode(){
	jQ("#sendCodebtn").text("发送验证").unbind("click").bind("click",sendVerCodeConfirm);
	jQ("#mobilenumipt").attr("disabled","").css({border:"1px solid #7f9db9"});
	jQ("#verifyCodebtn").unbind("click");
}

function sendVerCode(flag){
	var minput=jQ.trim(jQ("#mobilenumipt").val());
	if(flag){
		if(!minput.match(mobilever)){
			showAjaxCallBackMsg(0,"请输入正确的手机号码。");
			return;
		}
	}
	jQ.ajax({
		type:"POST",
		url:"userAjax!sendVerCodeAndReset.action",
		data:{
			mobilenum:minput
		},
		success:function(data){
			jQ("#viewopt").dialog("close");
			var dataObj=eval("("+data+")");
			showAjaxCallBackMsg(eval(dataObj.flag),dataObj.result);
			if(dataObj.flag){
				jQ("#sendCodebtn").text("重新发送");
				jQ("#mobilenumipt").css({border:"0px"}).attr("disabled","disabled");
				jQ("#sendCodebtn").unbind("click").bind("click",resetVerCode);
				jQ("#orientmobilenum").val("");
				jQ("#verifyCodebtn").unbind("click").bind("click",verCode);
			}
		}
	});
}

function verCode(obj){
	var minput=jQ.trim(jQ("#veript").val());
	if(!minput.match(mobilecode)){
		showAjaxCallBackMsg(0,"验证码只可能是6个数字。");
		return;
	}else{
		jQ.ajax({
			type:"POST",
			url:"userAjax!verifyCode.action",
			data:{
				codenum:minput
			},
			success:function(data){
				var dataObj=eval("("+data+")");
				showAjaxCallBackMsg(eval(dataObj.flag),dataObj.result);
				if(dataObj.flag){
					jQ("#modelchangebtn").removeClass("editon").text("修改");
					jQ(".editmode").hide();
					jQ("#verifyCodebtn").unbind("click");
					setTimeout(function(){
						jQ("#submitinfo").fadeOut();
					},2000);
				}
			}
		});
		
	}
}

function showAjaxCallBackMsg(flag,msg){
	if(flag){
		jQ("#submitinfo").removeClass("info").removeClass("error").addClass("info");
	}else{
		jQ("#submitinfo").removeClass("info").removeClass("error").addClass("error");
	}	
	jQ("#submitinfo").empty().append(msg).fadeIn();
}

jQ(function(){
	
	initEmailInfo();
	
	jQ("#viewopt").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		closeOnEscape:false,
		draggable:false,
		height:200,
		buttons: {
			
			"确定": function() {
				var opt=jQ("#viewopt");
				var triggerfun=opt.data("function");
				opt.trigger(triggerfun);
				opt.removeData("function");
				jQ("#viewopt_p").hide();
				jQ("#viewopt_img").show();
				//activeBtnOnDialog("viewopt",false);
				//jQ(this).dialog('close');
			},
			"取消": function() {
				var viewopt=jQ("#viewopt");
				var triggerfun=jQ("#viewopt").data("function");
				viewopt.unbind(triggerfun);
				viewopt.find("input.eventid").val("");
				viewopt.removeData("function");
				jQ(this).dialog('close');
			}
		},
		close: function() {
			jQ("#viewopt_p").show();
			jQ("#viewopt_img").hide();
		},
		open:function(){
		}
	});
});